
package com.ifcar.app.model;

import com.ifcar.app.util.HttpUtils;
import com.ifcar.app.util.ModelUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Topic {
    public static final String TOPIC_URL = "http://c.3g.163.com/nc/attitude/cover.html";

    public static final String TOPIC_ATTITUDE = "attitude";

    public static final String TOPIC_VOTE = "vote";

    public static final String PARAM_IMGSRC = "imgsrc";

    public static final String PARAM_NAME = "name";

    public static final String PARAM_REF = "ref";

    public static List<Map<String, Object>> getTopicList(Context context) {
        String ret = HttpUtils.getResponseFromGetUrl(TOPIC_URL, 30000, context);
        List<Map<String, Object>> list = toTopicList(ret);

        if (list != null && list.size() > 0) {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
            sp.edit().putString("topic_cache", ret).commit();
        } 
        return list;
    }

    public static List<Map<String, Object>> getCachedTopicList(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String rst = sp.getString("topic_cache", null);
        return toTopicList(rst);
    }
    
    public static List<Map<String, Object>> getDefaultTopicList() {
        String rst = "["
                + "{imgsrc:\"http://img3.cache.netease.com/cnews/2012/5/8/20120508013825a15bb.jpg\", name=\"深度\", ref=\"attitude\"},"
                + "{imgsrc:\"http://img4.cache.netease.com/3g/2012/5/8/2012050808000518db5.jpg\", name=\"评论\", ref=\"T1334130148846\"},"
                + "{imgsrc:\"http://img6.cache.netease.com/3g/2012/5/8/20120508095214f9454.jpg\", name=\"博客\", ref=\"T1334130296157\"},"
                + "{imgsrc:\"http://img4.cache.netease.com/3g/2012/5/8/20120508071426a865d.jpg\", name=\"论坛\", ref=\"T1334130320521\"},"
                + "{imgsrc:\"http://img4.cache.netease.com/3g/2012/5/7/201205072038555bf7c.jpg\", name=\"投票\", ref=\"vote\"}"
                + "]";

        return toTopicList(rst);
    }

    private static List<Map<String, Object>> toTopicList(String rst) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        if (TextUtils.isEmpty(rst)) {
            return list;
        }

        try {
            JSONArray array = new JSONArray(rst);
            if (array != null) {
                final int length = array.length();
                for (int i = 0; i < length; i++) {
                    JSONObject o = array.getJSONObject(i);
                    Map<String, Object> map = ModelUtils.json2Map(o);
                    list.add(map);
                }
            }

        } catch (Exception e) {
            list.clear();
        }

        return list;
    }
}
